//index.js
//获取应用实例
var app = getApp()
let httpUrl = app.globalData.httpUrl;
let thisErpId = app.globalData.thisErpId;
Page({
  data: {
    friendData: [],
    downBlock: true,
    page: 1,
    isLoad: true,
    pictures: [],
  },
  onLoad: function () {
    this.loadData();
  },
  loadData: function () {
    if (wx.showLoading) {
      wx.showLoading({
        title: '正在加载...',
      })
    }
    const page = this.data.page;
    wx.request({
      url: httpUrl + 'slsw/showfriends.do',
      method: 'POST',
      data: `weChatKey=9237B361957490BA9644A710A5E3DD84&&Data={is_refresh:'${page == 1}',page_num:'${page}',item_num:10,erpId:'${thisErpId}'}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8"'
      },
      success: (res) => {
        let list = res.data.Data;
        console.log(list)
        let isLoad = (list.length == 10) ? true : false;
        list = this.formatTime(list)
        this.setData({
          friendData: page == 1 ? list : [...this.data.friendData, ...list],
          isLoad: isLoad
        })
        wx.stopPullDownRefresh();
        wx.hideLoading();
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '服务器开了个小差，请稍后再试!',
          icon: 'loading'
        })
      }
    })
  },
  // 上拉加载
  // onReachBottom
  onReachBottom: function (e) {
    let isLoad = this.data.isLoad;
    if (isLoad == true) {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    } else {
      wx.hideLoading();
      wx.showToast({
        title: '已经没有更多数据了!',
        icon: 'clear'
      })
    }
  },
  //获取不到头像图片时采用默认图片
  errImg: function (e) {
    var _errImg = e.target.dataset.errImg;
    var _objImg = "'" + _errImg + "'";
    var _errObj = {};
    _errObj[_errImg] = "../../image/attr.png";
    this.setData(_errObj);//注意这里的赋值方式...
  },
  // 首页图片预览
  preimage: function (e) {
    let current = e.target.dataset.src;
    let index = e.target.dataset.index;
    let photos = this.data.friendData[index].photos;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: photos // 需要预览的图片http链接列表  
    })
  },

  downClose: function (e) {
    this.setData({
      downBlock: false,
    })
  },
  // 截取时间
  formatTime: function (arr) {
    for (let i = 0; i < arr.length; i++) {
      arr[i].create_time = (arr[i].create_time).substr(0,19);
    }
    return arr;
  },
  /**
  * 用户点击按钮分享
  */
  onShareAppMessage: function (res) {
    // if (res.from === 'button') {
    //   // 来自页面内转发按钮
    //   console.log(res.target)
    // }
    let name = res.target.dataset.name;
    let id = res.target.dataset.id;
    let img = res.target.dataset.img;
    return {
      title: name + '发布了一条消息',
      path: `/pages/sharewebview/sharewebview?friendId=${id}`,
      imageUrl: img,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
    /**
 * 下拉刷新
 */
  onPullDownRefresh: function () {
    let that = this;
    setTimeout(function(){
      that.setData({
        page: 1,
      })
      that.loadData();
    },1000)
  },
  /**
 * 用户点击右上角分享
 */
  // onShareAppMessage: function () {

  // }
  //更换页面显示
  // changePage: function (e) {
  //   let index = e.currentTarget.dataset.index;
  //   let text = e.currentTarget.dataset.text;
  //   this.setData({
  //     thisPage: index,
  //   })
  //   wx.setNavigationBarTitle({
  //     title: text
  //   })

  //   if (index == 2) {
  //     this.setData({
  //       searchPageType: 1
  //     })
  //     if ((this.data.blockData).length == 0) {
  //       this.getMaterial();        // 排行版获取
  //     }
  //   } else if (index == 3) {
  //     this.setData({
  //       searchPageType: 2
  //     })
  //     if ((this.data.slabData).length == 0) {
  //       this.getMaterial();        // 排行版获取
  //     }
  //   }
  // },

})
